﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QianBuo.Lottery.Web.Areas.Manage.Models;
using Code.Models;
using Code.Commen;
using Code.BLL;
using System.Web.Security;
using QianBuo.Lottery.Web.Areas.Manage.Models.Login;

namespace QianBuo.Lottery.Web.Areas.Manage.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Manage/Login/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                if (!model.CheckCode.Equals(TempData["Manage_Login"] as string, StringComparison.CurrentCultureIgnoreCase))
                {
                    ModelState.AddModelError("CheckCode", "验证码不正确");
                }
                else
                {
                    AdminManage AM = new AdminManage();
                    Admin admin;
                    var rs = AM.Login(model.LoginName, model.LoginPwd, Request.UserHostAddress,out admin);

                    if (rs != LoginResult.Normal)
                    {
                        ModelState.AddModelError("LoginError", rs.GetName());
                    }
                    else
                    {
                        Session["AdminID"] = admin.ID;
                        var aus = System.Configuration.ConfigurationManager.GetSection("system.web/authentication") as System.Web.Configuration.AuthenticationSection;
                        if (aus != null)
                        {
                            string str = string.Format("ManageLoginID:{0}&LoginName:{1}&RoleID:{2}&Roles:Manager", admin.ID, admin.LoginName, admin.RoleID);
                            FormsAuthenticationTicket tick = new FormsAuthenticationTicket(2, str, DateTime.Now, DateTime.Now.AddMinutes(aus.Forms.Timeout.TotalMinutes), false, "");
                            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(tick));
                            Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                            Response.Cookies.Add(cookie);
                        }

                        var a = Global.OnLineManagers[admin.ID];
                        if (a != null)
                        {
                            string str = string.Format("<script>alert('您的账号已在:{1}登录\\r\\n请尽快修改密码！');location.href='{0}';</script>", Request["returnUrl"] ?? Url.Action("", ""), a.LoginIP);
                            a.SessionID = Session.SessionID;
                            return Content(str);
                        }
                        //获取未处理提款申请条数
                        CacheManage.TakeMoneyUnDisposeCount = new TakeMoneyManage().GetTakeMoneyUnDisposeCount(null);

                        if (Request["ReturnUrl"] != null)
                        {
                            return Redirect(Request["ReturnUrl"]);
                        }

                        return RedirectToAction("", "");
                    }
                }
            }

            return View(model);
        }

        public ActionResult Out()
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            return RedirectToAction("Index");
        }
    }
}
